package com.dianrui.fmi.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ListView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.activity.KLineActivity;
import com.dianrui.fmi.adapter.HomeQuotesAdapter;
import com.dianrui.fmi.bean.HomeQutos;
import com.dianrui.fmi.util.CountDownTimerUtils;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.Url;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

//策略推荐
public class StrategyRecommendationFragment extends BaseFragment {

    private List<HomeQutos> mHomeQutoList = new ArrayList<>();
    private HomeQuotesAdapter homeQuotesAdapter;

    @BindView(R.id.listview)
    ListView listview;


    @Override
    public int getContentViewId() {
        return R.layout.frag_strategyrecommand;
    }

    @Override
    public void initView() {
        homeQuotesAdapter = new HomeQuotesAdapter(getActivity(), mHomeQutoList);
        listview.setAdapter(homeQuotesAdapter);
        getAllIndexData();
        homeQuotesAdapter.setOnButtonClickListener(new HomeQuotesAdapter.QuotesClickListener() {
            @Override
            public void onRegularClick(long tick, String name, String symbold, String type, String symbol, String fullsymbol, String price, String high, String low, String open, String zf, String yc, String buy1, String buy2, String position, String servicecharge, String zc) {
                Intent intent = new Intent(getActivity(), KLineActivity.class);
                Bundle bundle = new Bundle();
                bundle.putLong("tick", tick);
                bundle.putString("symbold", symbold);
                bundle.putString("name", name);
                bundle.putString("type", type);
                bundle.putString("symbol", symbol);
                bundle.putString("open", open);
                bundle.putString("high", high);
                bundle.putString("low", low);
                bundle.putString("fullsymbol", fullsymbol);
                bundle.putString("price", price);
                bundle.putString("zf", zf);
                bundle.putString("zc", zc);
                bundle.putString("type", "1");
                bundle.putString("yc", yc);
                bundle.putString("buy1", buy1);
                bundle.putString("buy2", buy2);
                bundle.putString("position", position);
                bundle.putString("servicecharge", servicecharge);
                intent.putExtras(bundle);
                startActivity(intent);
            }

        });
        detailsTimer.start();
    }

    CountDownTimerUtils detailsTimer = new CountDownTimerUtils(Long.MAX_VALUE, 5000) {
        @Override
        public void onTick(long millisUntilFinished) {
            getAllIndexData();


        }

        @Override
        public void onFinish() {

        }
    };

    @Override
    public void getData() {

    }


    private void getAllIndexData() {
        OkHttpUtils.getInstance().postWithMemberToken(Url.INDEX, "",  new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && isAdded()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                if (mHomeQutoList != null) {
                                    mHomeQutoList.clear();
                                }
                                JSONArray optional_index = jsonObject.optJSONObject("data").optJSONArray("top_symbols");
                                if(optional_index.length()>0) {
                                    for (int i = 0; i < optional_index.length(); i++) {
                                        HomeQutos homeQutos = new HomeQutos();
                                        homeQutos.symbols_id = optional_index.optJSONObject(i).optString("symbols_id");
                                        homeQutos.full_symbol = optional_index.optJSONObject(i).optString("full_symbol");
                                        homeQutos.symbol = optional_index.optJSONObject(i).optString("symbol");
                                        homeQutos.name = optional_index.optJSONObject(i).optString("name");
                                        homeQutos.type = optional_index.optJSONObject(i).optString("type");
                                        homeQutos.market = optional_index.optJSONObject(i).optString("market");
                                        homeQutos.tick = optional_index.optJSONObject(i).optLong("tick");
                                        homeQutos.i = optional_index.optJSONObject(i).optString("i");
                                        homeQutos.itd = optional_index.optJSONObject(i).optString("itd");
                                        homeQutos.price = optional_index.optJSONObject(i).optString("price");
                                        homeQutos.o = optional_index.optJSONObject(i).optString("o");
                                        homeQutos.h = optional_index.optJSONObject(i).optString("h");
                                        homeQutos.l = optional_index.optJSONObject(i).optString("l");
                                        homeQutos.yc = optional_index.optJSONObject(i).optString("yc");
                                        homeQutos.v = optional_index.optJSONObject(i).optString("v");
                                        homeQutos.zf = optional_index.optJSONObject(i).optString("zf");
                                        homeQutos.zd = optional_index.optJSONObject(i).optString("zd");
                                        homeQutos.buy1 = optional_index.optJSONObject(i).optString("buy1");
                                        homeQutos.buy2 = optional_index.optJSONObject(i).optString("buy2");
                                        homeQutos.position = optional_index.optJSONObject(i).optString("position");
                                        homeQutos.service_charge = optional_index.optJSONObject(i).optString("service_charge");
                                        mHomeQutoList.add(homeQutos);

                                    }
                                    if (homeQuotesAdapter != null) {
                                        homeQuotesAdapter.refresh(mHomeQutoList);
                                    }
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}
